package org.linzhu.Connect4;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.Properties;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import org.linzhu.Connect4.core.Connect4Exception;
import org.linzhu.Connect4.core.Player;
import org.linzhu.Connect4.core.Status;
import org.linzhu.Connect4.ui.Connect4CellRenderer;
import org.linzhu.Connect4.ui.Connect4TableModel;

/**
 *
 * @author zhulin
 */
public class Main extends JFrame {

    Connect4TableModel connect4TableModel = new Connect4TableModel();
    Connect4CellRenderer connect4CellRenderer = new Connect4CellRenderer();

    /** Creates new form MainFrame */
    public Main(String name) {
        super(name);
        initComponents();
        setCellSize(50);
        setKeyBindings();
        gameTable.setDefaultRenderer(Player.class, connect4CellRenderer);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {

        statusLabel = new javax.swing.JLabel();
        gameTable = new javax.swing.JTable();
        menuBar = new javax.swing.JMenuBar();
        gameMenu = new javax.swing.JMenu();
        newMenuItem = new javax.swing.JMenuItem();
        gameMenuItemSeparator = new javax.swing.JSeparator();
        quitMenuItem = new javax.swing.JMenuItem();
        helpMenu = new javax.swing.JMenu();
        aboutMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Lin's Connect4");
        setResizable(false);
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });

        statusLabel.setFont(statusLabel.getFont().deriveFont((statusLabel.getFont().getStyle() & ~java.awt.Font.ITALIC) | java.awt.Font.BOLD, statusLabel.getFont().getSize()-2));
        statusLabel.setText("Game in progress");
        getContentPane().add(statusLabel, java.awt.BorderLayout.PAGE_END);
        statusLabel.getAccessibleContext().setAccessibleName("");

        gameTable.setModel(connect4TableModel);
        gameTable.setFocusable(false);
        gameTable.setRowSelectionAllowed(false);
        getContentPane().add(gameTable, java.awt.BorderLayout.CENTER);

        gameMenu.setText("Game");

        newMenuItem.setText("New");
        newMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newMenuItemActionPerformed(evt);
            }
        });
        gameMenu.add(newMenuItem);
        gameMenu.add(gameMenuItemSeparator);

        quitMenuItem.setText("Quit");
        quitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                quitMenuItemActionPerformed(evt);
            }
        });
        gameMenu.add(quitMenuItem);

        menuBar.add(gameMenu);

        helpMenu.setText("Help");

        aboutMenuItem.setText("About");
        aboutMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aboutMenuItemActionPerformed(evt);
            }
        });
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        setJMenuBar(menuBar);

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void newMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newMenuItemActionPerformed
    connect4TableModel.newGame();
    gameTable.repaint();
}//GEN-LAST:event_newMenuItemActionPerformed

private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_LEFT) {
        connect4TableModel.moveHeaderPieceLeft();
    } else if (evt.getKeyCode() == KeyEvent.VK_RIGHT) {
        connect4TableModel.moveHeaderPieceRight();
    } else if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
        try {
            Status status = connect4TableModel.dropMarble();
            if (status == Status.Ongoing) {
                statusLabel.setText("Game in progress");
            } else if (status == Status.PlayerOneWin) {
                statusLabel.setText("Player 1 wins");
            } else if (status == Status.PlayerTwoWin) {
                statusLabel.setText("Player 2 wins");
            } else if (status == Status.Draw) {
                statusLabel.setText("It's a draw");
            } 
            gameTable.repaint();
        } catch (Connect4Exception ex) {
            statusLabel.setText(ex.getMessage());
        }
    }
}//GEN-LAST:event_formKeyPressed

private void aboutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutMenuItemActionPerformed
        String message = "Lin's Connect4\n";
        try {
            Properties pomProperties = new Properties();
            pomProperties.load(Main.class.getResourceAsStream("/META-INF/maven/zhulin/Connect4/pom.properties"));
            message += "version " + pomProperties.getProperty("version");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        JOptionPane.showMessageDialog(this, message, "About", JOptionPane.INFORMATION_MESSAGE);
}//GEN-LAST:event_aboutMenuItemActionPerformed

    private void quitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_quitMenuItemActionPerformed
        System.exit(0);
}//GEN-LAST:event_quitMenuItemActionPerformed

    private void setCellSize(int size) {
        gameTable.setRowHeight(size);
        for (int i = 0; i < gameTable.getColumnCount(); i++) {
            gameTable.getColumnModel().getColumn(i).setPreferredWidth(size);
        }
        pack();
    }
    
    private void setKeyBindings() {
        gameMenu.setMnemonic(KeyEvent.VK_G);
        newMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
        quitMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
        helpMenu.setMnemonic(KeyEvent.VK_H);
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) throws Exception {
        UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());        
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Main("Lin's Connect4").setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem aboutMenuItem;
    private javax.swing.JMenu gameMenu;
    private javax.swing.JSeparator gameMenuItemSeparator;
    private javax.swing.JTable gameTable;
    private javax.swing.JMenu helpMenu;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem newMenuItem;
    private javax.swing.JMenuItem quitMenuItem;
    private javax.swing.JLabel statusLabel;
    // End of variables declaration//GEN-END:variables
    
}
